package com.example.farmeri.ui.base;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.farmeri.R;
import com.example.farmeri.adapter.MyItemDecoration;
import com.example.farmeri.presenter.base.BasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Unbinder;

/**
 * Create by weiqishan on 2020/4/11.
 */
public class BaseFragment<K extends BasePresenter> extends Fragment {
    Toolbar mToolbar;

    protected K present;

    public Toolbar getToolbar() {
        return mToolbar;
    }

    protected void initToolBar(int v, String mTitle, Boolean isCenter, Boolean hasNav) {
        mToolbar = getView().findViewById(v);
        ((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToolbar.setTitle(mTitle);
        mToolbar.setTitleTextColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
        //返回导航
        if (hasNav){
            mToolbar.setNavigationContentDescription("返回");
            mToolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().finish();
                }
            });
        }
        //主标题居中
        if (isCenter){
            //设置标题居中
            TextView title = (TextView) mToolbar.getChildAt(0);
            title.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
            title.setGravity(Gravity.CENTER_HORIZONTAL);
        }

    }

    protected void recyclerViewAddData(RecyclerView recyclerView, RecyclerView.Adapter adapter, int orientation, int SpaceHeight) {
        MyItemDecoration decoration = new MyItemDecoration(SpaceHeight);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),orientation,false);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(decoration);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DefaultMethod(String Message){

    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    public void showToast(String msg){
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }
}
